package br.com.jogo.screens;

import br.com.jogo.MagesGame;
import br.com.jogo.services.Camera2D;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;

/**
 * Base class for Screen classes
 * 
 * @author diasleon
 */
public abstract class BaseScreen implements Screen, InputProcessor {

	public MagesGame game;
	public final Camera2D camera;
	
	private final Vector3 localPoint = new Vector3();

	public BaseScreen(MagesGame game) {
		super();
		this.game = game;
		camera = new Camera2D(game.GAME_WIDTH, game.GAME_HEIGHT);

		Gdx.input.setInputProcessor(this);
	}

	/**
	 * Main Loop
	 * 
	 * Divided in update and draw
	 */
	@Override
	public void render(float delta) {

		update(delta);

		// set camera
		camera.update();
		game.batcher.setProjectionMatrix(camera.combined);

		// the following code clears the screen with the given RGB color (black)
		Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		draw(game.batcher, delta);
	}

	public abstract void update(float delta);

	public abstract void draw(SpriteBatch spritebatch, float delta);

	@Override
	public void resize(int width, int height) {

	}

	@Override
	public void hide() {
		Gdx.app.log(MagesGame.LOG, "Hiding screen: ");

		// dispose the screen when leaving the screen;
		// note that the dipose() method is not called automatically by the
		// framework, so we must figure out when it's appropriate to call it
		dispose();
	}

	@Override
	public void pause() {


	}

	@Override
	public void resume() {


	}
	
	@Override
	public boolean keyDown(int keycode) {

		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	
	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * Implementation of touchUp method in order to adjust the touch coordinates
	 * to camera coordinates. The equivalent method is onTouchUp.
	 */
	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		Gdx.app.log(MagesGame.LOG, "touch up");
		localPoint.x = x;
		localPoint.y = y;
		camera.unproject(localPoint);
		
		return onTouchUp(localPoint.x, localPoint.y, pointer, button);
	}
	
	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		Gdx.app.log(MagesGame.LOG, "touch up");
		localPoint.x = x;
		localPoint.y = y;
		camera.unproject(localPoint);
		
		return onTouchDown(localPoint.x, localPoint.y, pointer, button);
	}
	
	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		
		localPoint.x = x;
		localPoint.y = y;
		camera.unproject(localPoint);
		
		return onTouchDragged(localPoint.x, localPoint.y, pointer);
	}

	public abstract boolean onTouchUp(float x, float y, int pointer, int button);
	
	public abstract boolean onTouchDown(float x, float y, int pointer, int button);
	
	public abstract boolean onTouchDragged(float x, float y, int pointer);
}
